Extension::MongoDBMapReduce Method

Syntax

.MapReduce as c (mapFunction as C [, reduceFunction as C [, finalizeFunction as C [, options as C]]])

Arguments

mapFunction

function definition to remap the results (grouping).

reduceFunction

function to reduce the collected results (i.e summarize)

finalizeFunction

Character

options

Character

Description

Get result of a map/Reduce against a mongo collection.

MapReduce allows processing of the data on the server to conform better to a desired result.

Get Companies by Country

dim mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","Northwinds","Customers")
? json_reformat(mongo.MapReduce("function() {emit(this.Country,this.CompanyName);}",""))
= [
    {
        "_id": "Germany",
        "value": [
            "Alfreds Futterkiste",
            "Blauer See Delikatessen",
            "Drachenblut Delikatessen",
            "Frankenversand",
            "Königlich Essen",
            "Lehmanns Marktstand",
            "Morgenstern Gesundkost",
            "Ottilies Käseladen",
            "QUICK-Stop",
            "Toms Spezialitäten",
            "Die Wandernde Kuh"
        ]
    },
    {
        "_id": "Mexico",
        "value": [
            "Ana Trujillo Emparedados y helados",
            "Antonio Moreno Taquería",
            "Centro comercial Moctezuma",
            "Pericles Comidas clásicas",
            "Tortuga Restaurante"
        ]
    },
	....

The map function here maps the CompanyName into a connection keyed on the Country.

Count Companies by Country

dim mongo as extension::MongoDB = extension::MongoDB::Create("mongodb://localhost:27017","Northwinds","Customers")
? json_reformat(mongo.MapReduce("function() {emit(this.Country,this.CompanyName);}","function(key,values) { return values.length; }"))
= [
    {
        "_id": "Germany",
        "value": 11
    },
    {
        "_id": "Mexico",
        "value": 5
    },
    {
        "_id": "UK",
        "value": 7
    },
	...

This example adds a second parameter to 'reduce' the data to a count of the companies found in each Country.